package com.huawei.boss.scripts;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;

import com.huawei.boss.lang.Strings;

public class ScriptEngineFactoryBean implements InitializingBean, FactoryBean {
    private static final Logger LOGGER = Logger.getLogger(ScriptEngineFactoryBean.class);
    private String enginename;
    private String extendsion;
    private String mimetype;
    private ScriptEngine engine;

    @Override
    public Object getObject() throws Exception {
        return this.engine;
    }

    @Override
    public Class getObjectType() {
        return ScriptEngine.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.engine = buildScriptEngine();

        if (this.engine == null) {
            throw new RuntimeException("could not find the script engine with [enginename:" + enginename
                    + ",extendsion:" + extendsion + ",mimetype:" + mimetype + "]");
        }

        if (LOGGER.isInfoEnabled()) {
            ScriptEngineFactory factory = this.engine.getFactory();
            LOGGER.info("Loading Script Engine:");
            LOGGER.info("Engine name: " + factory.getEngineName());
            LOGGER.info("Engine names: " + Strings.join(factory.getNames(), ","));
            LOGGER.info("Engine version: " + factory.getEngineVersion());
            LOGGER.info("Language name: " + factory.getLanguageName());
            LOGGER.info("Language version: " + factory.getLanguageVersion());
            LOGGER.info("Language extensions: " + Strings.join(factory.getExtensions(), ","));
            LOGGER.info("Language mimetypes: " + Strings.join(factory.getMimeTypes(), ","));
            LOGGER.info("Language thread feautre: " + factory.getParameter("THREADING"));
        }
    }

    private ScriptEngine buildScriptEngine() {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine tmpEngine = null;
        if (StringUtils.hasText(enginename)) {
            tmpEngine = manager.getEngineByName(enginename);
        }
        if (tmpEngine == null) {
            if (StringUtils.hasText(extendsion)) {
                tmpEngine = manager.getEngineByExtension(extendsion);
            }

            if (tmpEngine == null && StringUtils.hasText(mimetype)) {
                tmpEngine = manager.getEngineByMimeType(mimetype);
            }
        }
        return tmpEngine;
    }

    public String getEnginename() {
        return enginename;
    }

    public void setEnginename(String enginename) {
        this.enginename = enginename;
    }

    public String getExtendsion() {
        return extendsion;
    }

    public void setExtendsion(String extendsion) {
        this.extendsion = extendsion;
    }

    public String getMimetype() {
        return mimetype;
    }

    public void setMimetype(String mimetype) {
        this.mimetype = mimetype;
    }

}
